package mtons.cast.modules.controller;

import mtons.cast.modules.data.Result;
import mtons.cast.modules.play.PlayClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by langhsu on 2017/10/13.
 */
@Controller
@RequestMapping("/play")
public class PlayController {
    @Autowired
    private PlayClient playClient;
    @Autowired
    private SimpMessagingTemplate template;

    @GetMapping("/login")
    public String login(ModelMap model) {
        model.put("code_path", playClient.getMaker().getCode());

//        template.convertAndSend("/topic/notice", "test");

        return "/play/login";
    }

    @PostMapping("/login")
    public String postLogin(String username, String password, String code) {
        boolean logined = playClient.getMaker().login(username, password, code);
        playClient.setLogin(logined);
        return "redirect:/play/console";
    }

    @GetMapping("/console")
    public String console(ModelMap model) {
        model.put("logined", playClient.isLogin());
        return "/play/console";
    }

    @GetMapping("/start")
    @ResponseBody
    public Result start() {
        return Result.success(playClient.start());
    }

    @GetMapping("/stop")
    @ResponseBody
    public Result stop() {
        playClient.stop();
        return Result.success();
    }

}
